package com.gorkr.labuladong.data_struct.bt;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class M32CongShangDaoXiaDaYinErChaShuIcof {


    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int[] levelOrder(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            if(poll!=null){
                res.add(poll.val);
                if(poll.left!=null){
                    queue.offer(poll.left);
                }
                if(poll.right!=null){
                    queue.offer(poll.right);
                }
            }
        }
        return res.stream().mapToInt(Integer::valueOf).toArray();
    }


}
